// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.warehouse.migration;

import org.dengues.core.migration.AbstractMigrationService;
import org.dengues.core.migration.MigrationExecResult;
import org.dengues.core.resource.WarehouseResourceFactory;
import org.dengues.core.warehouse.ENodeCategoryName;
import org.dengues.model.warehouse.Storage;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf Qiang.Zhang.Adolf@gmail.com 2008-7-2 qiang.zhang $
 * 
 */
public class SetUUIDMigration extends AbstractMigrationService {

    /**
     * Qiang.Zhang.Adolf@gmail.com RenameEmfFileMigration constructor comment.
     */
    public SetUUIDMigration() {
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.core.migration.AbstractMigrationService#executeMigration()
     */
    @Override
    public MigrationExecResult executeMigration() {
        MigrationExecResult result = MigrationExecResult.SUCCESS_ALERT;
        for (ENodeCategoryName name : ENodeCategoryName.values()) {
            IFolder folder = WarehouseResourceFactory.getFolder(name);
            renameFolderStorage(name, folder);
        }
        return result;
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "renameFolderStorage".
     * 
     * @param name
     * @param folder
     */
    private void renameFolderStorage(ENodeCategoryName name, IFolder folder) {
        if (is(folder) && name.getExtension() != null) {
            try {
                IResource[] members = folder.members();
                for (IResource resource : members) {
                    if (resource instanceof IFile) {
                        Storage loadStorage = WarehouseResourceFactory.loadStorage(name, (IFile) resource);
                        if (loadStorage == null) {
                            break;
                        }
                        String uuid = loadStorage.getUuid();
                        String simpleName = WarehouseResourceFactory.getSimpleName(name, (IFile) resource);

                        if (uuid == null || !uuid.equals(simpleName)) {
                            uuid = simpleName;
                            loadStorage.setUuid(uuid);
                            loadStorage.setCategoryName(name.getName());
                            WarehouseResourceFactory.saveStorage(loadStorage);
                        }
                    } else if (resource instanceof IFolder) {
                        renameFolderStorage(name, (IFolder) resource);
                    }
                }
            } catch (CoreException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "is".
     * 
     * @param folder
     * @return
     */
    private boolean is(IFolder folder) {
        return folder != null && folder.exists();
    }

}
